<?php

namespace App\Console\Commands;

use App\Models\Punish;
use Illuminate\Console\Command;

class UserPunishCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:punish';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '处罚用户到期检测';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $punish_logs = Punish::where('end_time', '<=', date('Y-m-d H:i:s'))
            ->where('punish_status', 1)->get();

        foreach ($punish_logs as $punish_log) {
            $punish_log->user->update(['punish' => 0]);
            $punish_log->punish_status = 2;
            $punish_log->save();
        }

        return Command::SUCCESS;
    }
}
